package com.chuangke.sinotrukbudget.sdao;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuangke.common.enums.OrderStatusEnum;
import com.chuangke.common.utils.SecurityUtils;
import com.chuangke.core.exception.ChuangkeException;
import com.chuangke.flow.enums.FlowNodeStatusEnum;
import com.chuangke.sinotrukbudget.consts.BudgetContanst;
import com.chuangke.sinotrukbudget.dao.BudgetBpcPlanMapper;
import com.chuangke.sinotrukbudget.model.BudgetAccount;
import com.chuangke.sinotrukbudget.model.BudgetBpcPlan;
import com.chuangke.sinotrukbudget.util.BudgetUtil;

@Service
public class BudgetBpcPlanDaoService extends ServiceImpl<BudgetBpcPlanMapper, BudgetBpcPlan> {

    /**
     * 获取未启动流程的预算
     *
     * @return
     */
    public List<BudgetBpcPlan> findUnStartFlowData() {
        String taskSql = "select order_id from flow_hi_task_log ";

        QueryWrapper<BudgetBpcPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BudgetBpcPlan::getStatus, OrderStatusEnum.EDIT.getCode())
                .eq(BudgetBpcPlan::getAccountType, BudgetAccount.ACCOUNT_TYPE_CW)
                .notInSql(BudgetBpcPlan::getId, taskSql);

        BudgetUtil.appendRestrictQueryWrapperAudit(queryWrapper);

        Page<BudgetBpcPlan> page = new Page<>(1, 400);
        return this.page(page, queryWrapper).getRecords();
//		return this.list(queryWrapper);
    }

    /**
     * 获取未提交的预算
     *
     * @param category
     * @param deptId
     * @param timeList
     * @param accountIds
     * @return
     */
    public List<BudgetBpcPlan> findUnSubmitFlowData(String category, String deptId, String time,
                                                    List<String> accountIds) {
        List<String> timeList = BudgetUtil.getTimeList(category, time);
        QueryWrapper<BudgetBpcPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BudgetBpcPlan::getStatus, OrderStatusEnum.EDIT.getCode())
                .eq(BudgetBpcPlan::gettEntity, deptId).eq(BudgetBpcPlan::gettCategory, category)
                .in(BudgetBpcPlan::gettTime, timeList).in(BudgetBpcPlan::gettAccount, accountIds);
        return this.list(queryWrapper);
    }

    /**
     * 获取已审核或者未审核的预算数据(已审核；未审核)
     *
     * @param auditStatus
     * @param tCategory
     * @param time
     * @param deptIds
     * @param accountIds
     * @return
     */
    public List<BudgetBpcPlan> findAuditedOrUnAuditedData(String auditStatus, String tCategory, String time,
                                                          List<String> deptIds, List<String> accountIds, List<String> flowIds) {
        String userId = SecurityUtils.getUserId();
        String taskSql = null;

        if (BudgetContanst.AUDIT_STATUS_DSH.equals(auditStatus)) {// 待审核数据
            taskSql = "select order_id from flow_ru_task where flow_ru_task.user_id='" + userId
                    + "' and flow_ru_task.status='" + FlowNodeStatusEnum.unhandle.getCode() + "'";
        } else if (BudgetContanst.AUDIT_STATUS_YSH.equals(auditStatus)) {// 已审核数据
            taskSql = "select order_id from flow_hi_task_log where status='" + FlowNodeStatusEnum.agree.getCode()
                    + "' and user_id='" + userId + "'";
        } else {
            throw new ChuangkeException("参数status传参不对");
        }

        List<String> timeList = BudgetUtil.getTimeList(tCategory, time);

        QueryWrapper<BudgetBpcPlan> queryWrapper = new QueryWrapper<>();
        // 这个条件不加了，待提交的数据也可以从这里处理
//		if (BudgetContanst.AUDIT_STATUS_DSH.equals(auditStatus)) {
//			queryWrapper.lambda().eq(BudgetBpcPlan::getStatus, OrderStatusEnum.APPROVAL.getCode());
//		}
        queryWrapper.lambda().eq(BudgetBpcPlan::gettCategory, tCategory)
                .eq(BudgetBpcPlan::getAccountType, BudgetAccount.ACCOUNT_TYPE_CW).in(BudgetBpcPlan::gettTime, timeList);
        BudgetUtil.appendRestrictQueryWrapperAudit(queryWrapper);
        if (!CollectionUtils.isEmpty(deptIds)) {
            queryWrapper.lambda().in(BudgetBpcPlan::gettEntity, deptIds);
        }
        if (!CollectionUtils.isEmpty(accountIds)) {
            queryWrapper.lambda().in(BudgetBpcPlan::gettAccount, accountIds);
        }
        if (!CollectionUtils.isEmpty(flowIds)) {
            queryWrapper.lambda().in(BudgetBpcPlan::gettFlow, flowIds);
        }
        queryWrapper.lambda().inSql(BudgetBpcPlan::getId, taskSql);

        return this.list(queryWrapper);
    }

    /**
     * 获取编制界面的预算数据
     *
     * @param status
     * @param tCategory
     * @param deptId
     * @param time
     * @return
     */
    public List<BudgetBpcPlan> findEditBudgetData(String status, String tCategory, String deptId, String time,
                                                  List<String> accountIdList) {
        List<String> timeList = BudgetUtil.getTimeList(tCategory, time);

        QueryWrapper<BudgetBpcPlan> queryWrapper = new QueryWrapper<>();
        if (!StrUtil.isBlank(status) && !OrderStatusEnum.ALL.getCode().equals(status)) {
            queryWrapper.lambda().eq(BudgetBpcPlan::getStatus, status);
        }
        queryWrapper.lambda().eq(BudgetBpcPlan::gettEntity, deptId).eq(BudgetBpcPlan::gettCategory, tCategory)
                .eq(BudgetBpcPlan::getAccountType, BudgetAccount.ACCOUNT_TYPE_CW).in(BudgetBpcPlan::gettTime, timeList)
                .in(BudgetBpcPlan::gettAccount, accountIdList);
        BudgetUtil.appendRestrictQueryWrapperAudit(queryWrapper);

        return this.list(queryWrapper);
    }

    /**
     * 获取科目预算数据
     *
     * @param category
     * @param deptId
     * @param time
     * @return
     */
    public List<BudgetBpcPlan> findKmysBudget(String category, String deptId, String time) {
        List<String> timeList = BudgetUtil.getTimeList(category, time);
        QueryWrapper<BudgetBpcPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BudgetBpcPlan::getAccountType, BudgetAccount.ACCOUNT_TYPE_CW);
        queryWrapper.lambda().eq(BudgetBpcPlan::gettCategory, category).eq(BudgetBpcPlan::gettEntity, deptId);
        queryWrapper.lambda().in(BudgetBpcPlan::gettTime, timeList);
        queryWrapper.lambda().eq(BudgetBpcPlan::gettAudit, "INPUT_E3");// 审批线索只取INPUT_E3的，INPUT是业务预算；INPUT_E3是科目预算

        return this.list(queryWrapper);
    }

    /**
     * 用于非概算预算
     *
     * @param category
     * @param deptId
     * @param time
     * @return
     */
    public List<BudgetBpcPlan> findBudget(String category, String deptId, String time) {
        List<String> timeList = BudgetUtil.getTimeList(category, time);
        QueryWrapper<BudgetBpcPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BudgetBpcPlan::getAccountType, BudgetAccount.ACCOUNT_TYPE_CW);
        queryWrapper.lambda().eq(BudgetBpcPlan::gettCategory, category).eq(BudgetBpcPlan::gettEntity, deptId);
        queryWrapper.lambda().in(BudgetBpcPlan::gettTime, timeList);
        queryWrapper.lambda().eq(BudgetBpcPlan::gettAudit, "INPUT");

        return this.list(queryWrapper);
    }

    /**
     * 获取业务预算数据
     *
     * @param category
     * @param deptId
     * @param time
     * @return
     */
    public List<BudgetBpcPlan> findYwysBudget(String category, String deptId, String time) {
        List<String> timeList = BudgetUtil.getTimeList(category, time);
        QueryWrapper<BudgetBpcPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("T_ENTITY,T_CATEGORY,T_ACCOUNT,T_TIME,T_FLOW,sum(signdata) signdata");
        queryWrapper.groupBy("T_ENTITY", "T_CATEGORY", "T_ACCOUNT", "T_TIME", "T_FLOW");
        queryWrapper.lambda().eq(BudgetBpcPlan::gettCategory, category).eq(BudgetBpcPlan::gettEntity, deptId)
                .eq(BudgetBpcPlan::getAccountType, BudgetAccount.ACCOUNT_TYPE_CW);
        queryWrapper.lambda().in(BudgetBpcPlan::gettTime, timeList);
        queryWrapper.lambda().eq(BudgetBpcPlan::gettAudit, "INPUT");// 审批线索只取INPUT的，INPUT是业务预算；INPUT_E3是科目预算

        return this.list(queryWrapper);
    }

    /**
     * 根据条件查询存在流程的budget id
     *
     * @param deptId
     * @param category
     * @param accountId
     * @param time
     * @return
     */
    public String findFlowId(String deptId, String category, String accountId, String time) {
        List<String> timeList = BudgetUtil.getTimeList(category, time);
        String sql = "select order_id from flow_hi_task_log";
        QueryWrapper<BudgetBpcPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        queryWrapper.lambda().eq(BudgetBpcPlan::gettCategory, category).eq(BudgetBpcPlan::gettEntity, deptId)
                .eq(BudgetBpcPlan::gettAccount, accountId)
                .eq(BudgetBpcPlan::getAccountType, BudgetAccount.ACCOUNT_TYPE_CW).in(BudgetBpcPlan::gettTime, timeList)
                .inSql(BudgetBpcPlan::getId, sql);

        BudgetUtil.appendRestrictQueryWrapperAudit(queryWrapper);
        List<BudgetBpcPlan> bbpList = this.list(queryWrapper);
        return CollectionUtils.isEmpty(bbpList) ? "" : bbpList.get(0).getId();
    }

}
